package com.linuxone.iching.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.linuxone.iching.shared.AOU;
import com.linuxone.iching.shared.Explain;
import com.linuxone.iching.shared.Gua;

public class Iching implements EntryPoint {

	private final IChingServiceAsync ichingService = GWT
			.create(IChingService.class);

	public void onModuleLoad() {
		final Button sendButton = new Button("占");
		sendButton.addStyleName("sendButton");
		RootPanel.get("sendButtonContainer").add(sendButton);
		final Button clearButton = new Button("清除");
		clearButton.addStyleName("clearButton");
		RootPanel.get("clearButtonContainer").add(clearButton);

//		final DialogBox dialogBox = new DialogBox();
//		dialogBox.setAnimationEnabled(true);
//		dialogBox.setSize("800px", "500px");
		//final Button closeButton = new Button("Close");
		//closeButton.getElement().setId("closeButton");
		final HTML myTitle = new HTML();
		myTitle.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		final FlexTable myTable = new FlexTable();
		myTable.getElement().setId("FirstGUA");
		final FlexTable myTable2 = new FlexTable();
		myTable2.getElement().setId("SecondGUA");
		final VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(myTitle);
		dialogVPanel.add(myTable);
		dialogVPanel.add(myTable2);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		
		RootPanel.get("resultContainer").add(dialogVPanel);
		//dialogVPanel.add(closeButton);
//		dialogBox.setWidget(dialogVPanel);

//		closeButton.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				dialogBox.hide();
//				sendButton.setEnabled(true);
//				sendButton.setFocus(true);
//			}
//		});
		
		

		class MyHandler implements ClickHandler {
			public void onClick(ClickEvent event) {
				if(event.getSource().equals(sendButton))
					sendToServer();
				else
					clearResult();
			}
			
			private void clearResult(){
				myTitle.setHTML("");
				myTable.removeAllRows();
				myTable2.removeAllRows();
			}

			private void sendToServer() {
//				sendButton.setEnabled(false);
				
				ichingService.getGUA(
						new AsyncCallback<Gua>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
							}

							public void onSuccess(final Gua myGUA) {
								ichingService.getTouchAOU(new AsyncCallback<int[]>(){
									public void onFailure(Throwable caught) {
										// Show the RPC error message to the user
									}
									public void onSuccess(final int[] TouchAOU) {
										ichingService.getSecGUA(myGUA, TouchAOU, new AsyncCallback<Gua>(){
											public void onFailure(Throwable caught){
												// Show the RPC error message to the user
											}
											public void onSuccess(final Gua myGUA2){
												myTitle.setHTML("<b><font size=4>"+myGUA.getShortName()+"<img src=http://www.linuxone.org/iching/images/"+myGUA.getAou()+" align=absbottom>之"+myGUA2.getShortName()+"<img src=http://www.linuxone.org/iching/images/"+myGUA2.getAou()+" align=absbottom>卦</font></b>");
												createResultTable(myTable, myGUA, TouchAOU);
												createResultTable(myTable2, myGUA2, TouchAOU);
											}
										}
										);
									}
								}
								);
							}
						});
			}
		}
		
		

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		clearButton.addClickHandler(handler);
	}
	
	private void createResultTable(FlexTable myTable, Gua myGUA, int[] TouchAOU) {
		int row = 0;
		if(myTable.getElement().getId().compareTo("FirstGUA")==0){
			myTable.setText(row, 0, "主卦");
		} else {
			myTable.setText(row, 0, "之卦");
		}
		myTable.getFlexCellFormatter().setColSpan(row, 0, 2);
		myTable.getFlexCellFormatter().setHorizontalAlignment(row, 0, 
				HasHorizontalAlignment.ALIGN_CENTER);
		myTable.getFlexCellFormatter().setStyleName(row, 0, "guaHeader");
		
		row++;
		VerticalPanel ImgVPanel = new VerticalPanel();
		
		// myFront Img
		Image myFront = new Image("http://1.latest.linuxone-iching.appspot.com/images/s_"+myGUA.getFront());
		//myFront.setPixelSize(200, 270);
		ImgVPanel.add(myFront);
		
		Image myFrontFull = new Image("http://1.latest.linuxone-iching.appspot.com/images/"+myGUA.getFront());
		final PopupPanel myFrontPopup = new PopupPanel(true);
		myFrontPopup.setAnimationEnabled(true);
		myFrontPopup.setWidget(myFrontFull);
		myFrontFull.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				myFrontPopup.hide();
			}
		});
	    myFront.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	          myFrontPopup.center();
	        }
	    });

	    // myBack Img
		Image myBack = new Image("http://1.latest.linuxone-iching.appspot.com/images/s_"+myGUA.getBack());
		//myBack.setPixelSize(200, 270);
		ImgVPanel.add(myBack);
		
		Image myBackFull = new Image("http://1.latest.linuxone-iching.appspot.com/images/"+myGUA.getBack());
		final PopupPanel myBackPopup = new PopupPanel(true);
		myBackPopup.setAnimationEnabled(true);
		myBackPopup.setWidget(myBackFull);
		myBackFull.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				myBackPopup.hide();
			}
		});
	    myBack.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	          myBackPopup.center();
	        }
	    });
		
		myTable.setWidget(row, 0, ImgVPanel);
		myTable.getFlexCellFormatter().setVerticalAlignment(row, 0, HasVerticalAlignment.ALIGN_TOP);
		
		FlexTable rightTable = new FlexTable();
		int rightRow = 0;
		HorizontalPanel dialogHPanel = new HorizontalPanel();
		dialogHPanel.add(new Label(myGUA.getId()+"."));
		dialogHPanel.add(new Image("http://1.latest.linuxone-iching.appspot.com/images/"+myGUA.getAou()));
		dialogHPanel.add(new HTML("&nbsp;&nbsp;<b><font size=4>"+myGUA.getShortName()+"卦</font></b> <font size=2>("+myGUA.getFullName()+")</font>"));
		rightTable.setWidget(rightRow, 0, dialogHPanel);
		
		rightRow++;
		rightTable.setWidget(rightRow, 0, new HTML("<b>卦詞：</b>"+myGUA.getStatement().getOriginal()));
		//rightTable.getFlexCellFormatter().setColSpan(rightRow, 0, 2);
		
		rightRow++;
		rightTable.setWidget(rightRow, 0, new HTML("<b>翻譯：</b>"+myGUA.getStatement().getTranslate()));
		
		for(Explain myExplain : myGUA.getStatement().getExplains()) {
			rightRow++;
			rightTable.setWidget(rightRow, 0, new HTML("<b>解釋：</b>"+myExplain.getValue()));
		}
		
		int i = 0;
		for(AOU myAOU : myGUA.getAOUs().getAou()) {
			rightRow++;
			rightTable.setWidget(rightRow, 0, new HTML("<b>"+myAOU.getName()+":"+myAOU.getStatement().getOriginal()+"</b>"));
			if(i<6 && TouchAOU[i]==1)
				rightTable.getFlexCellFormatter().setStyleName(rightRow, 0, "touchAOU");
			for(Explain myExplain2 : myAOU.getStatement().getExplains()){
				rightRow++;
				rightTable.setWidget(rightRow, 0, new HTML(myExplain2.getValue()));
				if(i<6 && TouchAOU[i]==1)
					rightTable.getFlexCellFormatter().setStyleName(rightRow, 0, "touchAOU");
				break;
			}
			i++;
		}
		
		myTable.setWidget(row, 1, rightTable);
		myTable.getFlexCellFormatter().setVerticalAlignment(row, 1, HasVerticalAlignment.ALIGN_TOP);
		
		
	}
}
